//
//  RequestManager.m
//  Bunoff
//
//  Created by Stephen Tran on 5/4/14.
//  Copyright (c) 2014 STEPHENTRAN. All rights reserved.
//

#import "RequestManager.h"
#import <RestKit/RestKit.h>
#import "User.h"
@implementation RequestManager
static RequestManager* instance;

+ (RequestManager*)getInstance
{
    if (instance == nil) {
        instance = [[RequestManager alloc] init];
    }
    
    return instance;
}
- (void) getUser
{
    RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[User class]];
    [mapping addAttributeMappingsFromDictionary:@{
                                                  @"name":   @"fullName",
                                                  @"username":     @"username",
                                                  @"email":     @"email",
                                                  }];
    
    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping method:RKRequestMethodAny pathPattern:nil keyPath:nil statusCodes:nil];
    NSURL *url = [NSURL URLWithString:@"URL"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[responseDescriptor]];
    [operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
        NSLog(@"Profile FB: %@", [result array]);
    } failure:nil];
    [operation start];
}
- (void) postUser
{
    RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[User class]];
    [responseMapping addAttributeMappingsFromArray:@[@"fullName", @"username", @"email"]];
    NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); // Anything in 2xx
    RKResponseDescriptor *userDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping method:RKRequestMethodAny pathPattern:@"/user" keyPath:nil statusCodes:statusCodes];
    
    RKObjectMapping *requestMapping = [RKObjectMapping requestMapping]; // objectClass == NSMutableDictionary
    [requestMapping addAttributeMappingsFromArray:@[@"fullName", @"username", @"email"]];
    
    // For any object of class User, serialize into an NSMutableDictionary using the given mapping and nest
    // under the 'article' key path
    RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[User class] rootKeyPath:@"article" method:RKRequestMethodAny];
    
    RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"URL"]];
                                [manager addRequestDescriptor:requestDescriptor];
                                [manager addResponseDescriptor:userDescriptor];
                                
                                User *user = [User new];
                                user.fullName = @"Duong Tran";
                                user.username = @"stephentran";
                                user.email = @"stephentran84@gmail.com";
                                
                                // POST to create
                                [manager postObject:user path:@"/user" parameters:nil success:nil failure:nil];

}
- (void) deleteUser
{
    RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[User class]];
    [responseMapping addAttributeMappingsFromArray:@[@"fullName", @"username", @"email"]];
    NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); // Anything in 2xx
    RKResponseDescriptor *userDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping method:RKRequestMethodAny pathPattern:@"/user" keyPath:nil statusCodes:statusCodes];
    
    RKObjectMapping *requestMapping = [RKObjectMapping requestMapping]; // objectClass == NSMutableDictionary
    [requestMapping addAttributeMappingsFromArray:@[@"fullName", @"username", @"email"]];
    
    // For any object of class User, serialize into an NSMutableDictionary using the given mapping and nest
    // under the 'article' key path
    RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[User class] rootKeyPath:@"article" method:RKRequestMethodAny];
    
    RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"URL"]];
    [manager addRequestDescriptor:requestDescriptor];
    [manager addResponseDescriptor:userDescriptor];
    
    User *user = [User new];
    user.fullName = @"Duong Tran";
    user.username = @"stephentran";
    user.email = @"stephentran84@gmail.com";
    
    // POST to create
    [manager deleteObject:user path:@"/user/1" parameters:nil success:nil failure:nil];
    
}
- (void) updateUser
{
    RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[User class]];
    [responseMapping addAttributeMappingsFromArray:@[@"fullName", @"username", @"email"]];
    NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); // Anything in 2xx
    RKResponseDescriptor *userDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping method:RKRequestMethodAny pathPattern:@"/user" keyPath:nil statusCodes:statusCodes];
    
    RKObjectMapping *requestMapping = [RKObjectMapping requestMapping]; // objectClass == NSMutableDictionary
    [requestMapping addAttributeMappingsFromArray:@[@"fullName", @"username", @"email"]];
    
    // For any object of class User, serialize into an NSMutableDictionary using the given mapping and nest
    // under the 'article' key path
    RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[User class] rootKeyPath:@"article" method:RKRequestMethodAny];
    
    RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"URL"]];
    [manager addRequestDescriptor:requestDescriptor];
    [manager addResponseDescriptor:userDescriptor];
    
    User *user = [User new];
    user.fullName = @"Duong Tran";
    user.username = @"stephentran";
    user.email = @"stephentran84@gmail.com";
    
    // POST to create
    [manager patchObject:user path:@"/user/1" parameters:nil success:nil failure:nil];
    
}
- (void) testGetMehodOfRestKit
{
    RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[User class]];
    [mapping addAttributeMappingsFromDictionary:@{
                                                  @"name":   @"fullNameFB",
                                                  @"username":     @"usernameFB",
                                                  @"email":     @"emailFB",
                                                  }];
    
    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping method:RKRequestMethodAny pathPattern:nil keyPath:nil statusCodes:nil];
    NSURL *url = [NSURL URLWithString:@"https://graph.facebook.com/stephentran84"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[responseDescriptor]];
    [operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
        NSLog(@"Profile FB: %@", [result array]);
    } failure:nil];
    [operation start];
}


@end
